package com.inossem.warehousestorage_inossem.activity.inventory.chuangyou;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.extre.InventoryExtre;
import com.inossem.warehousestorage_inossem.adapter.inventory.chuangyou.InventoryPapersAdapter;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryPapersBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.chuangyou.InventoryPaperRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.List;

/**
 * 库存盘点 盘点凭证列表页
 *
 * @author 詹建宇
 * @time on 2018/3/29 16:06
 * @email jianyu.zhan@inossem.com
 */
public class InventoryPapersActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private PullableListView listView;

    private List<InventoryPapersBean.PaperBodyBean> list;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_inventory_listview);

        initView(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) findViewById(R.id.headerTitle);
        listView = (PullableListView) findViewById(R.id.listView);
        listView.setDividerHeight(ActivityUtils.dp2px(activity, 5));
        listView.setPullUpFlag(true);
        listView.setPullDownFlag(true);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                InventoryPapersBean.PaperBodyBean bean = list.get(i);
                Intent intent = new Intent(activity, InventoryPositionListActivity.class);
                intent.putExtra(InventoryExtre.STOCKTAKE_ID, bean.getStocktake_id());
                activity.startActivity(intent);
            }
        });
    }

    @Override
    public void initData(Activity activity) {
        title.setText("库存盘点");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
    }

    private void getData(Activity activity) {
        InventoryPaperRequest.request(activity, new GsonBeanListener<InventoryPapersBean>() {
            @Override
            public void onSuccess(InventoryPapersBean bean) {
                list = bean.getBody();
                InventoryPapersAdapter adapter = new InventoryPapersAdapter(activity, list);
                listView.setAdapter(adapter);
            }
        });
    }
}
